import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;



public class MyUtils {

	/**
	* The Function gets SQL from a file by Splitting on basis of ; if any semicolon in between it w
	* will throw an error
	* @author Sanjay yadav
	* @version 1.0
	* @since   2015-01-01
	*/
	public static ArrayList<String> getSQLFromFile(String filePath) throws IOException{
		
		FileReader f=new FileReader(filePath);
		
		BufferedReader bf=new BufferedReader(f);
		String out="";
		String lscon="";
		while((out=bf.readLine())!= null){
			lscon+=out;
			
		}
		ArrayList<String> S=new ArrayList<String>();
		for (String retval: lscon.split(";")){
		S.add(retval);
			
			
		}

		return S;
		
		
		}

	/**
	* The Function retrieves Filenaem from a text file ,
	* Such as in following sequence below:-
	*  
	* Table1
	* Table2
	* Table3
	* @author Sanjay yadav
	* @version 1.0
	* @since   2015-01-01
	*/
	public static ArrayList<String> getNameFromfile(String filename) throws FileNotFoundException{
		Scanner s;
		s = new Scanner(new File(filename));
		ArrayList<String> lsTBLNM=new ArrayList<String>();
		int count=0;
		while (s.hasNextLine()){
			lsTBLNM.add(s.nextLine());
			
			//System.out.println(s.nextLine());
			count=count+1;  
		}
		System.out.println(count);
		return lsTBLNM;
	}
	
	/**
	* The Function executes SQL from a SQL or text file using DBBean Class
	* @author Sanjay yadav
	* @version 1.0
	* @since   2015-01-01
	*/
	public static void executeSQLFromFile(ArrayList<String> lSSql) throws Exception, SQLException{
		 DBbean.connect();
		for (int i = 0; i < lSSql.size(); i++) {
            System.out.println("Executing :" +lSSql.get(i));
           
            DBbean.updateSQL(lSSql.get(i));
        }
		DBbean.close();
	}
	
	/**
	* The Function replaces any token from a String  and returns a String with tokens
	* replaced
	* @author Sanjay yadav
	* @version 1.0
	* @since   2015-01-01
	*/
	public static String replacTokenFromString(String lsString,String token){
		String tempSQl=lsString;
		return lsString.replace(token, tempSQl);
	}
	
	
	
}